package org.tity.sql.code;

import java.io.IOException;

import org.junit.Test;
import org.tity.commons.environment.ProjectInfo;
import org.tity.commons.io.EasyFile;
import org.tity.commons.log.Logger;
import org.tity.ends.boot.BootArgs;
import org.tity.sql.dao.base.TableDataTest;
import org.tity.sql.entity.builder.EntityCodeFactory;
import org.tity.sql.entity.builder.classbuiler.AbstractTableQueryBuilder;
import org.tity.sql.entity.builder.classbuiler.DefaultColumnMetas;
import org.tity.sql.meta.DBColumnMeta;
import org.tity.sql.meta.DBTableMeta;

import jodd.io.FileUtil;

public class TestEntityBuilder extends TableDataTest {

	public static final String ENTITY_DIR = "/tity-test-suite/src/org/tity/sql/code/entity";
	public static final String ENTITY_PACKAGE = "org.tity.sql.code.entity";
	public static final String BASE_CONFIG_FILE = "config-base.json";
	public static final String OVERRIDE_CONFIG_FILE = "config-override.json";

	/**
	 * 实体代码工厂
	 */
	private static EntityCodeFactory codeFactory;

	@Test
	public void buildEntitys() {
		// 初始化
		initialize();
		// 构建实体代码
		buildAll();
	}

	private void initialize() {

		// 创建代码工厂
		codeFactory = new EntityCodeFactory(dao) {
			/**
			 * 覆盖
			 */
			@Override
			public boolean isExcludeTable(String table) {
				boolean isExclude = super.isExcludeTable(table);
				isExclude = isExclude || table.toLowerCase().startsWith("qrtz_");
				isExclude = isExclude || !table.toLowerCase().startsWith("test_");
				return isExclude;
			}

		};

		// 设置无需生成实体的数据表
		codeFactory.addExcludeTable("qrtz_fired_triggers", "qrtz_paused_trigger_grps", "qrtz_scheduler_state",
				"qrtz_locks", "qrtz_simple_triggers", "qrtz_simprop_triggers", "qrtz_cron_triggers",
				"qrtz_blob_triggers", "qrtz_triggers", "qrtz_job_details", "qrtz_calendars");

		
		//严格模式，如果是严格模式必须有表注释和字段注释等
		codeFactory.setStrict(false);
		// 排除不需要创建实体的表
		codeFactory.addExcludeTablePrefix("qrtz_", "saaf_schedules");

		codeFactory.setDBCommentAllowBlank(false); // 必须要有数据库备注

		EasyFile entityDir = getEntityFileDir();

		codeFactory.setEntityDir(entityDir.file()); // 设置生成目录
		codeFactory.setEntityPkg(ENTITY_PACKAGE); // 设置包名
		System.out.println("entity:" + entityDir.getAbsolutePath());

		EasyFile baseFile = ProjectInfo.getMavenSourceDir(TestEntityBuilder.class).resolve(BASE_CONFIG_FILE);
		EasyFile overrideFile = ProjectInfo.getMavenSourceDir(TestEntityBuilder.class).resolve(OVERRIDE_CONFIG_FILE);
		codeFactory.setConfigFile(baseFile.file(),overrideFile.file());

		/**
		 * 配置默认列
		 */
		codeFactory.setDefaultColumnMetas(new DefaultColumnMetas() {
			@Override
			public void initColumns() {

				add(new DBColumnMeta(null, "id", 64,64, false, null, null, "ID,唯一标识", false, false, null, null, null));

				add(new DBColumnMeta(null, "creation_date", 64,64, false, null, null, "创建时间", false, false, null, null,
						null));
				add(new DBColumnMeta(null, "created_by", 64,64, false, null, null, "创建人", false, false, null, null, null));

				add(new DBColumnMeta(null, "last_update_date", 64,64, false, null, null, "修改时间", true, false, null, null,
						null));
				add(new DBColumnMeta(null, "last_updated_by", 64,64, false, null, null, "修改人", true, false, null, null,
						null));

				add(new DBColumnMeta(null, "delete_flag", 64,64, false, null, null, "是否删除", false, false, null, null,
						null));
				add(new DBColumnMeta(null, "delete_time", 64,64, false, null, null, "删除时间", true, false, null, null,
						null));
				add(new DBColumnMeta(null, "delete_by", 64,64, false, null, null, "删除人", true, false, null, null, null));

				add(new DBColumnMeta(null, "version_num", 64,64, false, null, null, "记录版本号,变更次数", true, false, null, null,
						null));

			}
		});

		/**
		 * 默认查询语句生成器
		 */
		codeFactory.setBasicTableQueryBuilder(new AbstractTableQueryBuilder() {
			@Override
			public String getQuery(String table) {
				DBTableMeta tm = codeFactory.getDAO().getTableMeta(table);

				// 如无特殊处理，则返回null，由框架自动完成
				DBColumnMeta c = tm.getColumn("deleted");
				if (c == null) {
					return null;
				}
				return "select * from " + table + " where deleted='N'";
			}
		});

	}

	public static void buildAll() {
		try {
			codeFactory.buildAll();
		} catch (Exception e) {
			Logger.exception(e);
		}
	}

	public EasyFile getEntityFileDir() {
		EasyFile dir = new EasyFile(BootArgs.getWorkDir());
		dir = dir.getParentFile().getParentFile().resolve(ENTITY_DIR);
		try {
			if (dir.exists()) {
				FileUtil.deleteDir(dir.file());
			}
		} catch (IOException e) {
			Logger.exception(e);
		}
		return dir;
	}

}
